//活动
exports.heatActivity = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var Organization_subscribe_course = 0;

    //任务流创建
    async.waterfall([

            //1.活动信息查询
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_activity + "/fa_id/" + restfulParams.fa_id,
                        method: 'POST',
                        data: {
                            fields: ["fa_id", "fa_title", "fa_content", "fa_end_time", "fa_collect_count", "fa_comment_count", "fa_read_score", "fa_share_score", "fa_join_score", "fa_browse_count", "fa_share_count", "fa_join_count", "fa_create_time"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.forumActivityData = data.data;
                        cb();
                    }
                );

            },
            //活动发布机构
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.forumActivityFixOrganization + "/fa_id/" + restfulParams.fa_id,
                        method: 'GET'
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        Organization_subscribe_course = data.data;
                        resultData.forumActivityOrganizationData = Organization_subscribe_course;


                        cb();
                    }
                );

            },
            //活动发布机构的约课
            function (cb) {
                if (Organization_subscribe_course.length > 0) {
                    // //console.log(Organization_subscribe_course[0].o_id);
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_course + "/oc_type/1/oc_oid/" + Organization_subscribe_course[0].o_id + "/skip/0/limit/1",
                            method: 'POST',
                            data: {
                                fields: ["oc_id", "oc_title", "oc_new_price", "oc_course_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            resultData.OrganizationSubscribeData = data.data;
                            cb();
                        }
                    );
                } else {
                    cb();
                }


            },
            //查询当前用户是否已关注当前机构
            function (cb) {
                if (Organization_subscribe_course.length > 0) {
                    if (restfulParams.u_id) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchOrganizationConcern + "/is_delete/0/uoc_oid/" + Organization_subscribe_course[0].o_id + "/uoc_uid/" + restfulParams.u_id,
                                method: 'POST',
                                data: {
                                    fields: ['uoc_id'].join(",")
                                }

                            },
                            function (err, data) {

                                var is_zan = data.data;

                                if (is_zan <= 0) {
                                    resultData.isConcernData = 0;
                                } else {
                                    resultData.isConcernData = 1;
                                }
                                cb();
                            }
                        );
                    } else {
                        cb();
                    }
                }else{
                    cb();
                }
            },
            //2.评论与回复
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_activity_comment + "/fac_faid/" + restfulParams.fa_id + "/fac_fid/0",
                        method: 'POST',
                        data: {
                            fields: [
                                "fac_id as fcc_id",
                                "fac_faid as fcc_faid",
                                "fac_uid as fcc_uid",
                                "fac_content as fcc_content",
                                "fac_reply_uid as fcc_reply_uid",
                                "fac_is_reply as fcc_is_reply",
                                "fac_zan_count as fcc_zan_count",
                                "fac_is_author_commnet as fcc_is_author_commnet",
                                "fac_create_time as fcc_create_time",
                                "fac_fid as fcc_fid",
                                "u_nickname",
                                "u_header_url"
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.activityCommentData = data.data;
                        cb();
                    }
                );

            },
            //查询当前用户是否对评论点赞
            function (cb) {
                if (restfulParams.u_id) {
                    //创建添加任务
                    var tasks = [];

                    //循环节点列表
                    resultData.activityCommentData.forEach(function (item, i) {

                        //创建每一个插入任务
                        tasks.push(function (cb) {

                            //查询对该用户的回复
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.searchActivityCommentZanRecord + "/is_delete/0/faczr_fid/" + item.fcc_id + "/faczr_uid/" + restfulParams.u_id,
                                    method: 'POST',
                                    data: {
                                        fields: ["faczr_id"].join(",")
                                    }
                                },
                                function (err, data) {

                                    var is_exist = data.data;

                                    if (is_exist.length <= 0) {
                                        item.isZan = 0;
                                    } else {
                                        item.isZan = 1;
                                    }
                                    cb();
                                });
                        });
                    });
                    async.waterfall(tasks, cb);
                } else {
                    cb();
                }
            },
            //2.评论与回复2
            function (cb) {

                //创建添加任务
                var tasks = [];

                //循环节点列表
                resultData.activityCommentData.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        //查询对该用户的回复
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_forum_activity_comment + "/fac_fid/" + item.fcc_id + "/count/1/skip/0/limit/2",
                                method: 'POST',
                                data: {
                                    fields: [
                                        "fac_id as fcc_id",
                                        "fac_content as fcc_content",
                                        "u_nickname",
                                    ].join(",")
                                }
                            },
                            function (err, data) {

                                item.replyCount = data.count;
                                item.replyData = data.data;

                                cb();
                            });

                    });

                });

                async.waterfall(tasks, cb);

            },
            //3.参与者积分排名
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_activity_join_user + "/fauj_faid/" + restfulParams.fa_id + "/skip/0/limit/10",
                        method: 'POST',
                        data: {
                            fields: ["u_nickname", "u_header_url", "fauj_integral"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.intergralData = data.data;
                        cb();
                    }
                );

            },
            //4.当前用户头像昵称
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_user_info + "/u_id/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["u_id", "u_nickname", "u_header_url"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.userData = data.data;
                        cb();
                    }
                );

            },
            //5.当前用户头积分、排名
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_user_integral_ranking,
                        method: 'POST',
                        data: {
                            fauj_uid: restfulParams.u_id
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.integralRankingData = data.data;
                        cb();
                    }
                );

            },
            //6.助力者昵称，助力分数
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_activity_helper_info + "/fauhr_join_id/" + restfulParams.u_id + "/skip/0/limit/1/sort_order/fauhr_create_time-desc",
                        method: 'POST',
                        data: {
                            fields: [
                                "fauhr_help_score", "u_nickname"
                            ].join(',')
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.helperData = data.data;
                        cb();
                    }
                );

            },
            //查询当前用户是否已收藏
            function (cb) {
                if (restfulParams.u_id) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/3/uc_fid/" + restfulParams.fa_id + "/uc_uid/" + restfulParams.u_id,
                            method: 'POST',
                            data: {
                                fields: ['uc_id'].join(",")
                            }

                        },
                        function (err, data) {

                            var is_zan = data.data;

                            if (is_zan <= 0) {
                                resultData.isCollectData = 0;
                            } else {
                                resultData.isCollectData = 1;
                            }
                            cb();
                        }
                    );
                } else {
                    cb();
                }

            },

            //查询当前用户是否已参与当前的活动
            function (cb) {
                if (restfulParams.u_id) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_activity_user_join + "/is_delete/0/fauj_faid/" + restfulParams.fa_id + "/fauj_uid/" + restfulParams.u_id,
                            method: 'POST',
                            data: {
                                fields: ['fauj_id'].join(",")
                            }

                        },
                        function (err, data) {

                            var is_join = data.data;

                            if (is_join <= 0) {
                                resultData.isJoinData = 0;
                            } else {
                                resultData.isJoinData = 1;
                            }
                            cb();
                        }
                    );
                } else {
                    cb();
                }

            }

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}